var app = getApp()

const RongIMLib = app.globalData.RongIMLib;

var params = {};
Page({
    data: {
        data: '',
        cattle: '',
        bg: '../../img/bg1.png',
        enterStatus:"",         // 入驻状态

		user_data:"",			// 个人信息
        message_num:0,          // 消息数量
    },
	// 获取用户详细信息
	getUserInfo(res){
		var self = this;
		wx.request({
			url: app.url.requestUrl + '/personal/userinfo',
			method: 'POST',
			data: {
				openid: res.data.openid,
				token: res.data.token,
			},
			success: function (datas) {
				if (datas.data.code == 1) {
					self.setData({
						data: datas.data.data
					})
				}
				if (datas.data.code == -1 || datas.data.code == 404) {
					self.loginOverdue()
				}
			}
		})
	},
    // 获取入驻状态
    getEnterStatus(res) {
        var self = this;
        var url = "/user/check_status";

        wx.request({
            url: app.url.requestUrl + url,
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
            },
            success: function (status) {
                if (status.data.code == 1) {
                    self.setData({
                        enterStatus: status.data.data
                    })
                }
            }
        })
    },
	// 登录过期状态
	loginOverdue(e) {
		var self = this;
		wx.removeStorage({
			key: 'user',
			success: function (res) {
				self.onLoad();
			},
		})
	},
    // 登录授权
    getWXUserInfo() {
        var self = this;
        //获取用户信息
        wx.getSetting({
            success: res => {
                if (!res.authSetting['scope.userInfo']) {
                    console.log(res,"未授权");
                    this.login.showModalBox()
                } else {
                    console.log(res,"已授权");
                    //已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success(res) {
                            self.uploadUserInfo(res.userInfo);
                        }
                    })
                }
            }
        })
    },
    // 确认授权登录
    __accreditEvent(e) {
        this.uploadUserInfo(e.detail.user_info)
        this.login.hideModalBox()
    },
    // 登录成功上传头像及用户信息
    uploadUserInfo(info) {
        var self = this;
        var url = "/user/save_user_info";
        var name = info.nickName;
        var header_img = info.avatarUrl;
        console.log(info)

		wx.setStorage({
			key: 'user_info',
			data: info,
		})
		this.setData({
			user_data: info
		})

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        name,
                        header_img
                    },
                    success: function (ress) {
						var datas = ress.data;
                        if (datas.code == 1) {
							self.getUserInfo(res)
                        }
                    }
                })
            }
        })
    },
    //生命周期函数--监听页面加载
    onLoad: function(options) {
        app.globalData.writeInContent = writeInContent;
        params.self = this;

        this.login = this.selectComponent("#login");

        var self = this;
        this.getWXUserInfo()
		wx.getStorage({
			key: 'user',
			success: function (res) {
				self.setData({
					cattle: res.data.is_cattle
				})
                self.getUserInfo(res)
                self.getEnterStatus(res)
			},
			fail() {
				wx.login({
					success: res => {
						wx.request({
							url: app.url.requestUrl + '/login/login',
							method: 'POST',
							data: {
								code: res.code
							},
							success: function (data) {
								var datas = data.data;
								if (data.data.code == 1) {
									wx.setStorage({
										key: 'user',
										data: data.data.data
									})
                                    self.getUserInfo(data.data)
                                    self.getEnterStatus(data.data)
								}
							}
						})
					}
				})
			}
		})
    },
    onShow(){
        writeInContent();
    }
})

function writeInContent() {

    var conversationTypes = [RongIMLib.ConversationType.PRIVATE];
    RongIMLib.RongIMClient.getInstance().getConversationUnreadCount(conversationTypes, {
        onSuccess: function (count) {
            // count => 多个会话的总未读数
            params.self.setData({
                message_num: count
            })
        },
        onError: function (error) {
            // error => 获取多个会话未读数错误码
        }
    });

}